package com.study.example.simpleclient.panel;

import com.study.example.simpleclient.ClientMain;
import com.sparrow.example_message.base.req.ReqLoginPacket;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginPanel extends JPanel implements ActionListener {

    private JLabel playerIdLabel;
    private JLabel nameLabel;
    private JLabel levelLabel;
    private JButton loginButton;

    public LoginPanel(long playerId, String name, int level) {
        setLayout(new GridBagLayout());

        playerIdLabel = new JLabel("玩家ID: " + playerId);
        nameLabel = new JLabel("名字: " + name);
        levelLabel = new JLabel("等级: " + level);

        add(playerIdLabel, createGBC(0, 0));
        add(nameLabel, createGBC(0, 1));
        add(levelLabel, createGBC(0, 2));

        loginButton = new JButton("登录");
        loginButton.addActionListener(this);
        add(loginButton, createButtonGBC(0, 3));
    }

    private GridBagConstraints createButtonGBC(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = 2; // 横跨两列（假设前面用了两列）
        gbc.insets = new Insets(10, 5, 10, 5);
        gbc.anchor = GridBagConstraints.CENTER;
        return gbc;
    }


    private GridBagConstraints createGBC(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        return gbc;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ReqLoginPacket req = new ReqLoginPacket();
        ClientMain.channel.writeAndFlush(req);
    }
}
